package com.fairytask.ui.item.chat
{
	import com.fairytask.event.ChatEvent;
	import com.fairytask.managers.ResManager;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	public class ChatListCheckBoxItem extends Sprite
	{
		private var _clickNum			:int		=0;
		private var _canUse			:Boolean	=true;//代表频道选项是否可以使用
		private var _skin				:String;
		private var _bgSpace		:Sprite;
		private var _unselectedSkin		:MovieClip;
		private var _selectedSkin		:MovieClip;
		private var _currentSkin	:MovieClip;
		private var _isEvent		:Boolean=	true;
		private var _selected      :Boolean= true;
		private var _id :int;
		private var _labName		:String;	//选项名字
		public function ChatListCheckBoxItem(_Skin:String,id:int,labName:String="")
		{
			super();
			_bgSpace			=	new Sprite();
			setId	=	id;
			addChild(_bgSpace);
			_bgSpace.buttonMode=true;
			skinStr(_Skin);
			_labName = labName;
			initEvent();
			
		}
		
		public function set canUse(canUse:Boolean):void
		{
			_canUse	=	canUse;
		}
		
		public function get canUse():Boolean
		{
			return _canUse;
		}
		
		public function set setId(id:int):void
		{
			_id	=id;
		}
		
		public function get id():int
		{
			return _id;
		}
		private function initEvent():void
		{
			_isEvent	=	true;
			_bgSpace.addEventListener(MouseEvent.CLICK,onClick);
			//临时屏蔽帮派
			/*if(_id!=4)
			{
				_bgSpace.addEventListener(MouseEvent.CLICK,onClick);
			}*/
		}
		//0代表当前频道项，7代表喇叭频道项
		private function onClick(e:MouseEvent):void
		{
			if(_id==0||_id==7)
			{
				dispatchEvent(new ChatEvent(ChatEvent.CHAT_LIST_CB_NOT_PIN,null,null,id.toString()));
			}
			else
			{
				_canUse		= 	_selected?false:true;
				selected	=	_selected?false:true;
				if(selected==false)
				{
					dispatchEvent(new ChatEvent(ChatEvent.CHAT_LIST_CB_PIN,null,null,id.toString()));
					_clickNum++;
				}
				if(selected==true&&_clickNum!=0)
				{
					dispatchEvent(new ChatEvent(ChatEvent.CHAT_LIST_CB_BACK_NOT_PIN,null,null,id.toString()));
				}
			}
		}
		private function skinStr(value:String):void
		{
			_skin = value;
			if(_skin=="")
			{
				drawBG();
			}else
			{
				var res:MovieClip	=	ResManager.newResClass(_skin) as MovieClip;
				_unselectedSkin		=	res.getChildByName("CheckBox_unselected") as MovieClip;
				_selectedSkin			=	res.getChildByName("CheckBox_selected") as MovieClip;
				//				resizeSkin();
				_unselectedSkin.x		=	_selectedSkin.x    =	0;
				_unselectedSkin.y		=	_selectedSkin.y    =	0;
				_bgSpace.addChild(_selectedSkin);
				_currentSkin	=	_selectedSkin;
			}
		}
		private function drawBG():void
		{
			_bgSpace.graphics.clear();
			_bgSpace.graphics.beginFill(0xff0000,0);
			_bgSpace.graphics.drawRect(0,0,0,0);
			_bgSpace.graphics.endFill();
		}
		private function clearBG():void
		{
			while(_bgSpace.numChildren>0)
			{
				_bgSpace.removeChildAt(0);
			}
		}
		
		public function get selected():Boolean
		{
			return _selected;
		}
		
		public function set selected(value:Boolean):void
		{
			_selected = value;
			clearBG();
			if(_selected)
			{
				_bgSpace.addChild(_selectedSkin);
				_currentSkin	=	_selectedSkin;	
			}
			else
			{
				_bgSpace.addChild(_unselectedSkin);
				_currentSkin	=	_unselectedSkin;
				
			}
		}
		
		public function get labName():String
		{
			return _labName;
		}
	}
	
}